package com.maila.calltaxi.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.maila.calltaxi.R;
import com.maila.calltaxi.utils.NullCheckUtil;
import com.maila.calltaxi.utils.RegularUtils;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.autolayout.utils.AutoUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 修改手机号pop
 */

public class ChangeClintPop extends PopupWindow {


    private View mMenuView;
    String phone = "";

    ViewHolder vh;


    public ChangeClintPop(final Context context, final ChangeClinetListener changeClinetListener) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.view_change_client, null);
        AutoUtils.autoSize(mMenuView);
        vh = new ViewHolder(mMenuView);
        mMenuView.setTag(vh);
        vh.btnNext.setClickable(false);
        vh.btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        vh.btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(NullCheckUtil.checkNullPoint(phone)){
                    changeClinetListener.editphonenum(phone);
                    dismiss();
                }
            }
        });

        vh.etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                        if (RegularUtils.isPhoneNumber(s.toString())) {
                            vh.btnNext.setBackgroundResource(R.color.text_three);
                            vh.btnNext.setClickable(true);
                            phone = s.toString();

                        } else {
                            vh.btnNext.setClickable(false);
                            vh.btnNext.setBackgroundResource(R.color.drive_line);
                        }
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
//            设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//            设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.sharepopupanimation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(80000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                return true;
            }
        });
    }
public interface  ChangeClinetListener{
    void editphonenum(String num);
}
    static class ViewHolder {
        @BindView(R.id.btn_cancle)
        AutoRelativeLayout btnCancle;
        @BindView(R.id.btn_next)
        TextView btnNext;
        @BindView(R.id.et_code)
        EditText etCode;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
